import { fileURLToPath, URL } from 'node:url'
import { dynamicChunkPlugin } from 'vite-plugin-dynamic-chunk';
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import viteCompression from "vite-plugin-compression"
import {viteMockServe} from "vite-plugin-mock"
const SPLIT_EXPERIENCE_MODULES = {
  vue: ["vue", "vue-router"],
  elementPlus: ["element-plus"],
};
// https://vite.dev/config/
export default defineConfig({
  build: {
    minify: "terser",
    // css压缩
    cssMinify: true,
    // 生成sourcemap 用来找到代码问题位置
    // sourcemap:true,111
    // css切割 在文件中时@import 导入css单独切割
    cssCodeSplit: true,
    terserOptions: {
      compress: {
        // 移除console
        drop_console: true,
        // 移除debugger
        drop_debugger: true,
        // 移除未使用console.log
        pure_funcs: ['console.log']
      },
      format: {
        // 去除注释
        comments: false
      }
    }
  },
  plugins: [
    vue(),
    vueDevTools(),
    AutoImport({
      resolvers: [ElementPlusResolver()],
      imports: ["vue", "vue-router"],
      dts: "auto-imports.d.ts"
    }),
    Components({
      resolvers: [ElementPlusResolver()],
      dirs: ["src/views", "src/components"],
      extensions: ["vue"],
      dts: "components.d.ts"
    }),
    dynamicChunkPlugin({
      dependencySplitOption: SPLIT_EXPERIENCE_MODULES
    }),
    viteCompression({
      // 默认即可，是否在控制台显示压缩信息
      verbose: true,
      // 默认即可，是否禁用插件
      disable: true,
      // 默认10240字节（10KB），只有大小超过此阈值的资源才会被处理。
      // 注意这里指的是资源原始大小，不是gzip后的体积。
      threshold: 10240,
      // 使用gzip压缩
      algorithm: 'gzip',
      // 生成的压缩包后缀
      ext: ".gz"
    }),
    viteMockServe()
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    },
  },
  optimizeDeps: {

  },
  server: {
    proxy: {
      '/api': {
        target: 'http://localhost:4000',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, ''),
      },
      '/weather': {
        target: 'http://apis.juhe.cn',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/weather/, ''),
      },
    }
  }
})
